package com.chen.im.view;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * @Description
 * @Author Pengwei
 * @CreatDate 2021/11/15 01:35
 */
public class ChatListItem {
    @FXML
    Label msgContent;

    @FXML
    public void initialize() {
        initClickItem();
    }

    public void setData(String data) {
        DropShadow ds = new DropShadow();
        ds.setOffsetY(2.0);
        ds.setColor(Color.color(0.4, 0.4, 0.4));
        msgContent.setText(data);
        msgContent.setEffect(ds);

    }

    //点击弹出编辑框
    private void initClickItem() {
        msgContent.setOnMouseClicked(event -> {
            Label label = (Label) event.getSource();
            Stage stage = new Stage();
            TextArea textArea = new TextArea(label.getText());
            textArea.setWrapText(true);
            textArea.setStyle("-fx-padding: 1;-fx-background-color: white;-fx-border-width: 5;-fx-border-color: white");
            //计算同面积下求5:3宽高比的边长 width*height = 7a * 4a ==> a*a = 面积/15 开方函数 Math.sqrt(n) 求a
            double width = Math.sqrt((label.getWidth() * label.getHeight() / 28)) * 7 + 40 ;
            double height = Math.sqrt((label.getWidth() * label.getHeight() / 28)) * 4 + 20;
            Scene s = new Scene(textArea, width, height);

            s.getStylesheets().add("css/ChatEdit.css");
            stage.setScene(s);
            stage.setX(event.getScreenX() - (width/2));
            stage.setY(event.getScreenY() - (height/2) - 20);
            stage.setTitle("点击复制");
            stage.show();
            //监听弹窗的焦点变换,如果取消焦点则关闭弹窗
            stage.focusedProperty().addListener((observable, oldValue, newValue) -> {
                if (!newValue) {
                    stage.close();
                }

            });
        });
    }
}
